<%@page import="study.top.mydb.items.Category2Item"%>
<%@page import="study.top.mydb.Category2"%>
<%@page import="study.top.mydb.items.Category1Item"%>
<%@page import="java.util.ArrayList"%>
<%@page import="study.top.mydb.Category1"%>
<%@page import="study.top.mydb.items.ProductItem"%>
<%@page import="study.top.mydb.Product"%>
<%@page import="study.top.common.PatternChecker"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ include file="dbcon.jsp"%>
<%

	
	String docid = web.getValue("docid");

	if (!PatternChecker.getInstance().isValue(docid)) {
		out.print("<script type='text/javascript'>alert('파라미터가 없습니다.');history.back();</script>");
		return;
	}
	/** 상품데이터 조회 */
	Product Product = new Product();
	ProductItem item = Product.searchParser(docid);

	if (item == null) {
		out.print("<script type='text/javascript'>alert('데이터 조회에 실패했습니다. 잠시후 다시 시도해 주세요.');");
		return;
	}
	db.connect();
	// 카테고리1 목록에 대한 ArrayList 받아오기
	Category1 dept1 = new Category1();
	ArrayList<Category1Item> Ca1_list = dept1.selectCategory1List();
	
	// 카테고리2 목록에 대한 ArrayList 받아오기
	//Category2 dept2 = new Category2();
	//ArrayList<Category2Item> Ca2_list = dept2.selectCategory2List(1);

%>
<!DOCTYPE html>
<html lang="ko">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"/>
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
		<title>여기에 제목을 입력하세요.</title>

		<!-- 모바일 웹 페이지 설정 -->
		<link rel="shortcut icon" href="assets/ico/favicon.png" />
		<link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png" />
		<link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png" />
		<link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png" />
		<link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png" />

		<!-- bootstrap -->
		<link rel="stylesheet" type="text/css" href="assets/css/bootstrap.min.css" />
		<!-- 나눔고딕 웹 폰트 적용 -->
		<link rel="stylesheet" type="text/css" href="assets/css/nanumfont.css" />
		<!-- 반응형 웹을 지원하지 않을 경우 -->
		<!-- <link rel="stylesheet" href="assets/css/non-responsive.css" /> -->

		<!-- IE8 이하 버전 지원 -->
		<!--[if lt IE 9]>
		<script type="text/javascript" src="assets/js/html5shiv.js"></script>
		<script type="text/javascript" src="assets/js/respond.min.js"></script>
		<![endif]-->

		<!-- IE10 반응형 웹 버그 보완 -->
		<!--[if gt IE 9]>
		<link rel="stylesheet" type="text/css" href="assets/css/ie10.css" />
		<script type="text/javascript" src="assets/js/ie10.js"></script>
		<![endif]-->

		
	</head>

	<body>
		<div class="container">
		<div class="page-header">
			<h1>상품 정보 보기</h1>
		</div>

		<form id="frmProductView" name="frmProductView" class="form-horizontal" role="form"
			method="post" action="aProductAddOK.jsp">
			
			<!--  상품번호 자동입력  -->
        	<input type="hidden" name="prono" value="<%=docid %>">
        	<input type="hidden" name="pdate" value="<%=item.getPublish_date() %>">
        	
			<fieldset>

				<!-- 입력양식 -->
				<div class="form-group">
					<label for="name" class="col-md-2 control-label">상품 명</label>
					<div class="col-md-10">
						<input type="text" class="form-control" id="name"
							placeholder="상품명을 입력하세요" name="name" value="<%=item.getTitle() %>">
					</div>
				</div>

				<div class="form-group">
					<label for="userid" class="col-md-2 control-label">이미지</label>
					<div class="col-md-10">
						<p class="form-control-static">
							<img width="200" height="200" src="<%=item.getImgurl()%>">
							<input type="hidden" id="imgurl" name="imgurl" value="<%=item.getImgurl()%>" />
						</p>
					</div>
				</div>

				<div class="form-group">
					<label for="grade" class="col-md-2 control-label">정보</label>
					<div class="col-md-10">
						<input type="text" class="form-control" id="Decription"
							placeholder="정보를 입력하세요" name="Decription" value="<%=item.getDecription() %>">
					</div>
				</div>

				<div class="form-group">
					<label for="birthdate" class="col-md-2 control-label">브랜드</label>
					<div class="col-md-10">
						<input type="text" class="form-control" id="Brand"
							placeholder="브랜드를 입력하세요" name="Brand" value="<%=item.getBrand() %>">
					</div>
				</div>

				<div class="form-group">
					<label for="tel" class="col-md-2 control-label">카테고리1</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getC1_name()%></p>
					</div>
				</div>
				<div class="form-group">
					<label for="seq" class="col-md-2 control-label">카테고리1</label>
					<div class="col-md-10">
						<select id="ca1seq" name="ca1seq" class="form-control">
							<option value="0">---- 선택하세요 ----</option>
							<%
								if (Ca1_list != null) {
									for (int i=0; i<Ca1_list.size(); i++) {
										Category1Item c1_item = Ca1_list.get(i);
							%>
							<option value="<%=c1_item.getSeq()%>"><%=c1_item.getC1_name()%></option>
							<%
									}
								}
							%>
						</select>
					</div>
				</div>

				<div class="form-group">
					<label for="height" class="col-md-2 control-label">카테고리2</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getC2_name()%>
						</p>
					</div>
				</div>
				<div class="form-group">
					<label for="deptno" class="col-md-2 control-label">카테고리2</label>
					<div class="col-md-10">
						<select id="ca2seq" name="ca2seq" class="form-control">
							<option value="0">---- 선택하세요 ----</option>
							
						</select>
					</div>
				</div>

				<div class="form-group">
					<label for="weight" class="col-md-2 control-label">최고가</label>
					<div class="col-md-10">
						<input type="number" class="form-control" id="Price_max"
							placeholder="최고가를 입력하세요" name="Price_max" value="<%=item.getPrice_max() %>">
					</div>
				</div>

				<div class="form-group">
					<label for="deptno" class="col-md-2 control-label">최저가</label>
					<div class="col-md-10">
						<input type="number" class="form-control" id="Price_min"
							placeholder="최저가를 입력하세요" name="Price_min" value="<%=item.getPrice_min() %>">
					</div>
				</div>

				<div class="form-group">
					<label for="profno" class="col-md-2 control-label">제조사</label>
					<div class="col-md-10">
						<input type="text" class="form-control" id="Maker"
							placeholder="제조사를 입력하세요" name="Maker" value="<%=item.getMaker() %>">
					</div>
				</div>

				<div class="form-group">
					<div class="col-md-offset-2 col-md-10">
						<div class="row">
							<div class="col-md-4">
								<!--  <a href="aProductAddOK.jsp" class="btn btn-primary btn-block">상품추가</a>-->
								<button type="submit" id="Add" formaction="aProductAddOK.jsp"
									formmethod="post"
									class="btn btn-primary btn-block">상품추가</button>
							</div>
							<div class="col-md-4">
								<a href="javascript:history.back();" class="btn btn-primary btn-block">뒤로가기</a>
							</div>
						</div>
					</div>
				</div>

			</fieldset>
		</form>
	</div>

	<!-- Javascript -->
	<script src="assets/js/jquery.js"></script>
	<script src="assets/js/bootstrap.min.js"></script>

	<script type="text/javascript">
		$(function() {
			$("#frmProductView").submit(function() {					
				// 아이디 입력여부 검사
				if (!$("input[name='name']").val().isValue()) {
					alert("카테고리를 입력하세요.");
					$("input[name='name']").focus();
					return false;
				}
				if (!$("input[name='Decription']").val().isValue()) {
					alert("카테고리를 입력하세요.");
					$("input[name='Decription']").focus();
					return false;
				}
				if ($("select[name='ca1seq']").val() == 0) {
					alert("구분을 선택하세요.");
					$("select[name='ca1seq']").focus();
					return false;
				}
				if ($("select[name='ca2seq']").val() == 0) {
					alert("구분을 선택하세요.");
					$("select[name='ca2seq']").focus();
					return false;
				}
				if (!$("input[name='Brand']").val().isValue()) {
					alert("카테고리를 입력하세요.");
					$("input[name='Brand']").focus();
					return false;
				}
				if (!$("input[name='name']").val().isValue()) {
					alert("카테고리를 입력하세요.");
					$("input[name='name']").focus();
					return false;
				}
				if (!$("input[name='Price_max']").val().isValue()) {
					alert("최고가를 입력하세요.");
					$("input[name='Price_max']").focus();
					return false;
				}
				if (!$("input[name='Price_max']").val().isNum()) {
					alert("최고가를 입력하세요.");
					$("input[name='Price_max']").focus();
					return false;
				}
				if (!$("input[name='Price_min']").val().isValue()) {
					alert("최소가를 입력하세요.");
					$("input[name='Price_min']").focus();
					return false;
				}
				if (!$("input[name='Price_min']").val().isNum()) {
					alert("최소가를 입력하세요.");
					$("input[name='Price_min']").focus();
					return false;
				}
				if (!$("input[name='Maker']").val().isValue()) {
					alert("제조사를 입력하세요.");
					$("input[name='Maker']").focus();
					return false;
				}
			});
		});
	</script>
	<script type="text/javascript">
			$(function(){
				$("select[name='ca1seq']").change(function(){
					$("#ca2seq").empty();			
					var c1seq = $(this).find("option:selected").val();
					
					$.ajax({
					    url: "ajax/Category2.jsp",
					    dataType : "xml",
					    data : "c1seq=" + c1seq,
					    timeout: 30000,
					    cache : false,
					    success: function(req){
					    	// option 태그에 대한 반복
					    	$(req).find("option").each(function(){
					    		// seq 와 name 태그를 찾아 변수 선언에 값 넣기
					    		var seq = $(this).find("seq").text();
					    		var name = $(this).find("name").text();
					    		
					    		// 옵션 태그를 만들고 value 와 text 를 지정하여 값을 지정한 곳에 넣기
								var option = document.createElement('option');
									option.value = seq;
									option.text = name;
									// frmProductView 폼 안에 ca2seq 에 옵션 태그를 넣기
									document.forms['frmProductView'].elements['ca2seq'].add(option);
								});
							},
							error : function(xhr, status, error) {
								alert("status : " + status + "/ error-code: "
										+ xhr.status + " " + error);
						}
					});
				});
			});
	</script>
</body>
</html>